package com.zhanjiangyifang.shop.base;

/**
 * Created by admin on 2018/7/4.
 */


public class BasePresenter<V extends BaseView> implements Presenter<V> {

    /**
     * 当前连接的View
     */
    protected V baseView;

    /**
     * Presenter与View建立连接
     * @param baseView 与此Presenter相对应的View
     */
    @Override
    public void attachView(V baseView) {
        this.baseView = baseView;
    }

    /**
     * Presenter与View连接断开
     */
    @Override
    public void detachView() {
        this.baseView = null;
    }

    /**
     * 是否与View建立连接
     * @return
     */
    public boolean isViewAttached() {
        return baseView != null;
    }

    /**
     * 获取当前连接的View
     * @return
     */
    public V getBaseView() {
        return baseView;
    }

    /**
     * 每次调用业务请求的时候都要先调用方法检查是否与View建立连接，没有则抛出异常
     */
    public void checkViewAttached() {
        if (!isViewAttached()) {
            throw new BaseViewNotAttachedException();
        }
    }

    public static class BaseViewNotAttachedException extends RuntimeException {
        public BaseViewNotAttachedException() {
            super("请求数据前请先调用 attachView(BaseView) 方法与View建立连接");
        }
    }
}

